import {retrievePassword} from '../../../utils/api/module/login';
import { getLoginSendcode } from "../../../utils/api/module/login"
Page({
    data: {
        codeText:"获取验证码",
        currentTime: 60,
        disabled: true,
        // 第一步验证中的电话号吗
        phoneVal: null,
        // 验证吗输入
        codeVal: null,
        //第一步是否完成
        isFirsted: true,
        password: null,
        newPassword: null,

        //零时数据
        lsCode: 897654,


    },
    onLoad: function (options) {

    },
    /**
     * 双向绑定电话号
     * @param e
     */
    getPhoneVal(e){
        this.setData({
            phoneVal: e.detail.value,
        })
    },
    /**
     * 双向绑定验证码
     * @param e
     */
    getCodeVal(e){
        this.setData({
            codeVal: e.detail.value,
        })
    },
    /**
     * 点击获取验证码
     */
    getCodeApi(){
        let  phone = this.data.phoneVal;
        if(!this.data.disabled){
            return false;
        }else if(phone == null){
            wx.showToast({
                title: '请输入电话号',
                icon: 'error'
            })
            return false;
        }else if(!(/^1[345678]\d{9}$/.test(phone))){
            wx.showToast({
                title: '号码格式有误',
                icon: 'error'
            })
            return false;
        }else{
            this.setData({
                disabled: false
            })
            this.times();
            // 调用API获取验证码, 然后赋值给 lsCode， 后边验证
            getLoginSendcode({
                mobile: phone
            }).then(res => {
                if(res.data.code == 0) {
                    wx.showToast({title: res.data.message, icon: 'none'});
                    return false;
                };
                if(res.data.code == 1) {
                    this.data.lsCode = res.data.data;
                };
            });
        }
    },
    /**
     * 获取验证码倒计时
     * @param options
     */
    times: function(options) {
        let that = this;
        let currentTime = that.data.currentTime
        let interval = setInterval(function() {
            currentTime--;
            that.setData({
                codeText: currentTime + '秒后重试'
            })
            if (currentTime <= 0) {
                clearInterval(interval) //清除定时器
                that.setData({ //对定时器重新设置
                    codeText: '重新发送',
                    currentTime: 60,
                    disabled: true
                })
            }
        }, 1000)
    },
    nextclick(){
        let code = this.data.codeVal;
        let apiCode = this.data.lsCode;
        if(!apiCode) {
            wx.showToast({
                title: '验证码不能为空',
                icon: 'error'
            })
            return false;
        }
        if(code != apiCode){
            wx.showToast({
                title: '验证码错误',
                icon: 'error'
            })
            return false;
        }else{
            console.log('开始下一步');
            this.setData({
                isFirsted: true
            })
        }
    },
    /**
     * 双向绑定新密码
     */
    getPasswordVal(e){
        let name = e.currentTarget.dataset.name,
            val = e.detail.value;
        this.setData({ [name]: val });
    },
    confirmClick(){
        let data = this.data;
        let num = /^(\d+[a-zA-Z]+|[a-zA-Z]+\d+)([0-9a-zA-Z]*)$/; // 所含字母数
        if(!data.originalpass) {
            wx.showToast({ title: "请输入原密码",  icon: "none" });
               this.data.requestFlag = false;
            return false;
        };
        if(data.newpass.length != data.againnewpass.length) {
            wx.showToast({ title: "新密码和确认密码不一致",  icon: "none" });
               this.data.requestFlag = false;
            return false;
        };
        if(data.newpass.length < 6 || data.newpass.length > 16 ) {
            wx.showToast({ title: "请输入6~16位密码",  icon: "none" });
               this.data.requestFlag = false;
            return false;
        };
        if(data.newpass.indexOf(' ') != -1) {
            wx.showToast({ title: "密码不能包含空格",  icon: "none" });
            this.data.requestFlag = false;
            return false;
        };
        if(!num.test(data.newpass)) {
            wx.showToast({ title: "所输入的密码中必须含有数字和字母",  icon: "none" });
            this.data.requestFlag = false;
            return false;
        };
        retrievePassword({
            originalpass: data.originalpass,
            newpass: data.newpass,
            againnewpass: data.againnewpass
        }).then(res => {
            wx.showToast({ title: res.data.message, icon: 'none' });
            if(res.data.code == 1) {
                wx.reLaunch({ url: 'pages/enterprise/index/index' });
            };
        });
    }
});